package com.lh_travel.lohas.fragment;

import android.annotation.SuppressLint;
import android.util.Log;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.lh_travel.lohas.R;
import com.lh_travel.lohas.base.UnLazyBasefragment;
import com.lh_travel.lohas.widget.NestedScrollWebView;

@SuppressLint("ValidFragment")
public class WebFragment extends UnLazyBasefragment {

    private String mHomeUrl;
    private FrameLayout fl_container;
    private NestedScrollWebView mWebView = null;

    public WebFragment() {
    }

    public WebFragment(String mHomeUrl) {
        this.mHomeUrl = mHomeUrl;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_web;
    }

    @Override
    protected void findView() {
        //  mWebView = findViewByID(R.id.webview);

        fl_container = findViewByID(R.id.fl_container);
    }

    @Override
    protected void initView() {

        mWebView = mApp.mWebView;

        fl_container.addView(mWebView);

        setWebview();
        initHardwareAccelerate();
    }

    @Override
    protected void initData() {

        mWebView.loadUrl(mHomeUrl);

        mWebView.setWebChromeClient(new WebChromeClient());
    }

    private void setWebview() {
        WebSettings webSettings = mWebView.getSettings();
        //如果访问的页面中要与Javascript交互，则webview必须设置支持Javascript
        webSettings.setJavaScriptEnabled(true);

        //设置自适应屏幕，两者合用
        webSettings.setUseWideViewPort(true); //将图片调整到适合webview的大小
        webSettings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小

        //缩放操作
        webSettings.setSupportZoom(true); //支持缩放，默认为true。是下面那个的前提。
        webSettings.setBuiltInZoomControls(true); //设置内置的缩放控件。若为false，则该WebView不可缩放
        webSettings.setDisplayZoomControls(false); //隐藏原生的缩放控件

        //其他细节操作
        webSettings.setCacheMode(WebSettings.LOAD_DEFAULT); //关闭webview中缓存
        webSettings.setAllowFileAccess(true); //设置可以访问文件
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true); //支持通过JS打开新窗口
        webSettings.setLoadsImagesAutomatically(true); //支持自动加载图片
        webSettings.setDefaultTextEncodingName("utf-8");//设置编码格式

        webSettings.setDomStorageEnabled(true);
        webSettings.setNeedInitialFocus(true);

    }

    private void initHardwareAccelerate() {
        try {
            if (Integer.parseInt(android.os.Build.VERSION.SDK) >= 11) {
                getActivity().getWindow().setFlags(
                        android.view.WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
                        android.view.WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
            }
        } catch (Exception e) {

        }
    }

    @Override
    public void onDestroy() {

        if (mWebView != null) {
            fl_container.removeView(mWebView);
            mWebView = null;
        }

        super.onDestroy();
    }

    @Override
    public void onDestroyView() {

        if (mWebView != null) {
            fl_container.removeView(mWebView);
            mWebView = null;
        }

        super.onDestroyView();
    }
}
